package cn.hsa.fqz.task.service.impl;

import cn.hsa.fqz.task.controller.vo.SysCodeQueryVO;
import cn.hsa.fqz.task.controller.vo.SysCodeVO;
import cn.hsa.fqz.task.dao.SysCodeMapper;
import cn.hsa.fqz.task.entity.RegionDO;
import cn.hsa.fqz.task.entity.SysCode;
import cn.hsa.fqz.task.entity.TTaskProcess;
import cn.hsa.fqz.task.service.SysCodeService;
import cn.hsa.fqz.task.service.dto.RegionDTO;
import cn.hsa.fqz.task.service.dto.TaskMedcClueFeedbackStatisticBasicDTO;
import cn.hsa.hsaf.core.framework.util.PageResult;
import cn.hsa.hsaf.core.framework.web.WrapperResponse;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @version 1.0
 * @date 2023-11-21 17:12
 */
@Slf4j
@Service
public class SysCodeServiceImpl implements SysCodeService {

    @Autowired
    private SysCodeMapper sysCodeMapper;

    @Override
    public List<SysCode> getCodeByField(String field) {
        return sysCodeMapper.selectByField(field);
    }

    @Override
    public List<SysCode> getCodeByPid(String pid) {
        return sysCodeMapper.getCodeByPid(pid);
    }

    @Override
    public PageResult<SysCode> selectSysCodeByPage(SysCodeQueryVO vo) {
        if (vo.getPage() == null) {
            return null;
        }
        Page<SysCode> page =  PageHelper.startPage(vo.getPage().getPageNum(), vo.getPage().getPageSize());
        List<SysCode> list = sysCodeMapper.selectPageByCondition(vo.getCondition());

        PageResult<SysCode> result = new PageResult<>();
        result.setData(list);
        result.setPageNum(page.getPageNum());
        result.setPageSize(page.getPageSize());
        result.setPages(page.getPages());
        result.setRecordCounts((int)page.getTotal());
        return result;
    }

    @Override
    public Boolean insertSysCodeInfo(SysCodeVO vo) {
        SysCode code = new SysCode();
        BeanUtils.copyProperties(vo,code);
        return sysCodeMapper.insertSelective(code)>0;
    }

    @Override
    public Boolean updateSysCodeInfo(SysCodeVO vo) {
        SysCode code = new SysCode();
        BeanUtils.copyProperties(vo,code);
        return sysCodeMapper.updateSelective(code)>0;
    }

    @Override
    public Boolean deleteSysCodeInfo(Integer id) {
        sysCodeMapper.deleteByPrimaryKey(id);
        return true;
    }

    @Override
    public List<String> selectAllFields() {
        return sysCodeMapper.selectAllFields();
    }

    @Override
    public Map<String, List<SysCode>> getCodeByFields(List<String> fields) {
        List<SysCode> sysCodes = sysCodeMapper.selectListByFields(fields);
        Map<String, List<SysCode>> result = sysCodes.stream().collect(Collectors.groupingBy(SysCode::getField));
        return result;
    }
}
